package math;

import javax.media.j3d.Transform3D;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

public class Transforms {

	public static Transform3D makePivotRotation(Point3d pivot, Vector3d camY, Vector3d camZ, double deltaX, double deltaY) {
		Transform3D moveToCenter = new Transform3D();
		Vector3d translation = new Vector3d(pivot);
		translation.negate();
		moveToCenter.setTranslation(translation);
		
		Transform3D moveBack = new Transform3D();
		moveBack.invert(moveToCenter);
		
		AxisAngle4d axis = new AxisAngle4d(camZ, Math.toRadians(-deltaY));
		Transform3D rotateZ = new Transform3D();
		rotateZ.set(axis);
		
		axis = new AxisAngle4d(camY, Math.toRadians(-deltaX));
		Transform3D rotateY = new Transform3D();
		rotateY.set(axis);
		
		Transform3D transform = new Transform3D();
		transform.setIdentity();
		transform.mul(moveBack, rotateZ);
		transform.mul(rotateY);
		transform.mul(moveToCenter);
		
		return transform;
	}
	
	public static Transform3D makeAxisRotation(Point3d pos, Vector3d axis, double angle) {
		AxisAngle4d axisAngle = new AxisAngle4d(axis, Math.toRadians(angle));
		Transform3D rotation = new Transform3D();
		rotation.set(axisAngle);
		
		Transform3D moveToCenter = new Transform3D();
		Vector3d translation = new Vector3d(pos);
		translation.negate();
		moveToCenter.setTranslation(translation);
		
		Transform3D moveBack = new Transform3D();
		moveBack.invert(moveToCenter);
		
		Transform3D transform = new Transform3D();
		transform.mul(moveBack, rotation);
		transform.mul(moveToCenter);
		
		return transform;
	}
	
	public static Transform3D makeTransformAboutPoint(Point3d pos, Transform3D transform) {
		Transform3D moveToCenter = new Transform3D();
		Vector3d translation = new Vector3d(pos);
		translation.negate();
		moveToCenter.setTranslation(translation);
		
		Transform3D moveBack = new Transform3D();
		moveBack.invert(moveToCenter);

		Transform3D combined = new Transform3D();
		combined.mul(moveBack, transform);
		combined.mul(moveToCenter);
		
		return combined;
	}
}
